//
//  UserManager.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/3.
//  Copyright © 2018 LKGood. All rights reserved.
//

import Foundation
import SwiftyJSON

class UserManager {
    
    // MARK: - Singleton
    static let shared = UserManager()
    private init() {}
    
    private let currentUserKey = "USER_MANAGER_CURRENT_USER"
    
    var currentUser: User? {
        set {
            let userDefaults = UserDefaults.standard
            userDefaults.set(newValue?.getDictionary(), forKey: currentUserKey)
            userDefaults.synchronize()
        }
        
        get {
            let userDefaults = UserDefaults.standard
            if let userDictionary = userDefaults.dictionary(forKey: currentUserKey) {
                let user = User(dictionary: userDictionary)
                return user
            } else {
                return nil
            }
        }
    }
    
    public func updateUserDefault() {
        let userDefaults = UserDefaults.standard
        userDefaults.set(self.currentUser?.getDictionary(), forKey: currentUserKey)
        userDefaults.synchronize()
    }
}
